﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BattleScenelui : MonoBehaviour
{
    public static BattleScenelui _instance;
    /*---控件---*/
    public Text[] texts;//章节、分数、目标
    public Image djs;//倒计时的黄条
    public Transform iocnpos;//不间断打败个数显示的位置
    public GameObject[] pans;//胜利、失败、退出
    public GameObject[] jiangli;//胜利之后的奖励展示
    public GameObject dropEquipment;//掉落装备
    public GameObject dropMoney;//掉落金钱
    public GameObject cd;//技能cd
    bool cd1 = false;
    float cdvalue1 = 1;
    /*---变量---*/
    private int score=0;//分数
    private float timevalue;//时间
    [HideInInspector]
    public bool iscd = false;

    private void Awake()
    {
        _instance = this;
        VariableDocument.GetInstance.dropped_equipment = 0;
        VariableDocument.GetInstance.dropped_equipment_id = new int[]{ -1, -1, -1, 10 };
        //DataManager.GetInstance.DataInit();

    }
    
    private void Start()
    {
        cd.SetActive(false);
        DataInit();
        timevalue = VariableDocument.GetInstance.timernum;
        jiangli[0].SetActive(false);
        jiangli[1].SetActive(false);
        jiangli[2].SetActive(false);
    }

    private void Update()
    {
        if (cd1)
        {
            cd.gameObject.SetActive(true);
            cdvalue1 -= 0.05f*Time.deltaTime;
            cd.GetComponent<Image>().fillAmount = cdvalue1; // / 1;
            if (cdvalue1 <= 0)
            {
                cd.gameObject.SetActive(false);
                cd1 = false;
                cdvalue1 = 1;
            }
        }
        if (Const.isgameover)
            return;
        if (iscd)
        {
            timevalue -= Time.deltaTime;
            if (timevalue > 0)
            {
                djs.fillAmount = timevalue / VariableDocument.GetInstance.timernum;
            }
            else
            {
                OpenSbpan();
            }
        }
    }

    private void DataInit()
    {
        texts[0].text = VariableDocument.GetInstance.checkpoint_name[VariableDocument.GetInstance.current_checkpoint];
        texts[1].text = LanguageData._instance.currentscore + "0";
        texts[2].text = LanguageData._instance.currentlv + VariableDocument.GetInstance.killtager;
    }

    public void AddScore()
    {
        score+=10;
        texts[1].text = LanguageData._instance.currentscore + score;
        if(score> DataManager.GetInstance.gameData.topscore)
        {
            DataManager.GetInstance.gameData.topscore = score;
        }
    }

    public void Reducekill()
    {
        VariableDocument.GetInstance.killtager--;
        texts[2].text = LanguageData._instance.currentlv + VariableDocument.GetInstance.killtager;
        if(VariableDocument.GetInstance.killtager == 0)
        {
            StartCoroutine(OpenSl());
        }

    }

    IEnumerator OpenSl()
    {
        yield return new WaitForSeconds(0.35f);
        OpenSlpan();
    }


    public void AttackBtn()
    {
        //GameControl._Instance.PlayerAttack();
    }

    public void CreateLj(int num)
    {
        GameObject game = DataManager.GetInstance.Loadasset(ResourcesEnum.Prefab, "Lianji");
        GameObject temp = Instantiate(game, this.transform, false);
        temp.transform.position = iocnpos.position;
        temp.GetComponent<LianjiUI>().Init(num);
    }
    //胜利
    public void OpenSlpan()
    {
        if (!Const.isgameover)
        {
            Const.isgameover = true;
            if (VariableDocument.GetInstance.killtager == 0 && VariableDocument.GetInstance.current_checkpoint == VariableDocument.GetInstance.max_checkpoint)
            {
                VariableDocument.GetInstance.max_checkpoint += 1;
            }
            pans[0].SetActive(true);
            int zz = 0;
            for(int fr = 0; fr < 4; fr++)
            {
                if(fr == 3)
                {
                    jiangli[3].SetActive(true);
                    jiangli[3].transform.GetChild(0).GetComponent<Text>().text = "X" + VariableDocument.GetInstance.dropped_equipment_id[3];
                    VariableDocument.GetInstance.money += VariableDocument.GetInstance.dropped_equipment_id[3];
                }
                else if(VariableDocument.GetInstance.dropped_equipment_id[fr] != -1)
                {
                    jiangli[zz].SetActive(true);
                    jiangli[zz].transform.GetChild(0).GetComponent<Image>().sprite = GetNewItem._instance.item[VariableDocument.GetInstance.dropped_equipment_id[fr]].itemImage;
                    GetNewItem._instance.GetNew(VariableDocument.GetInstance.dropped_equipment_id[fr]);
                    zz++;
                }
            }
        }
    }
    //失败
    public void OpenSbpan()
    {
        if (!Const.isgameover)
        {
            Const.isgameover = true;
            pans[1].SetActive(true);
        }
    }
    //退出
    public void OpenTcpan()
    {
        Const.isgameover = true;
        pans[2].SetActive(true);
    }
    //关闭退出
    public void CloseTcpan()
    {
        Const.isgameover = false;
        GameControl._Instance.CreateEnemy();
        pans[2].SetActive(false);
    }
    //返回
    public void BackBtnEvent()
    {
        GoToInterface.ToInterface("10MainLine");
    }
    //下一关
    public void ClickXYG()
    {

        VariableDocument.GetInstance.current_checkpoint += 1;
        VariableDocument.GetInstance.timernum = 60 - VariableDocument.GetInstance.current_checkpoint * 3;
        VariableDocument.GetInstance.killtager = 20 + VariableDocument.GetInstance.current_checkpoint * 5;
        GoToInterface.ToInterface("BattleScene");
       
    }
    //双击
    float t1, t2;
    public void DoubleClick()
    {
        cd1 = true;
        print("click");
        GameObject[] objects = GameObject.FindGameObjectsWithTag("enemy") as GameObject[];
        foreach (GameObject a in objects)
        {
            int k = Random.Range(1, 5);
            if (k == 1||k==2)
            {
                AddScore();
                Reducekill();
                a.transform.GetChild(1).GetChild(2).gameObject.SetActive(true);
            }
        }
        Invoke("noSkill", 1f);
    }
    void noSkill()
    {
        //skill101.SetActive(false);
        GameObject[] objects = GameObject.FindGameObjectsWithTag("enemy") as GameObject[];
        foreach (GameObject a in objects)
        {
            if (a.transform.GetChild(1).GetChild(2).gameObject.activeInHierarchy)
            {
                Destroy(a.gameObject);
            }
            //a.transform.GetChild(1).GetChild(2).gameObject.SetActive(false);
        }
    }



}
